package com.demo.boss.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.boss.common.convert.PayWayConvert;
import com.demo.boss.common.enums.SysCode;
import com.demo.boss.entity.PayWay;
import com.demo.boss.models.PageRequest;
import com.demo.boss.models.Result;
import com.demo.boss.models.request.PayWayConfigRequest;
import com.demo.boss.models.request.PayWayRequest;
import com.demo.boss.models.vo.DicItemVo;
import com.demo.boss.server.PayWayService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class PayWayController {
    @Resource
    private PayWayService payWayService;
    @Resource
    private PayWayConvert payWayConvert;
    @GetMapping("/payWay/pageList")
    public Result pageList(String productCode,PageRequest pageRequest){
        IPage<PayWay> payWayIPage = payWayService.pageList(productCode,pageRequest);
        Map<String,Object> data = new HashMap<>();
        data.put("current",payWayIPage.getCurrent());
        data.put("pages",payWayIPage.getPages());
        data.put("list",payWayConvert.payWayVoList(payWayIPage.getRecords()));
        return Result.ok(data);
    }
    /**
     * 删除支付方式
     */
    @PostMapping("/payWay/remove")
    public Result payWayRemove(@RequestBody @Validated PayWayRequest payWayRequest){
        payWayService.removePayWay(payWayRequest);
        return Result.ok();
    }
    /**
     * 支付方式字典查询
     */
    @GetMapping("/payWay/dic")
    public Result queryPayWayDic(){
        List<DicItemVo> dicItemVoList =  payWayService.queryPayWayDic();
        return Result.ok(dicItemVoList);
    }
    /**
     * 支付类型
     */
    @GetMapping("/paytype/dic")
    public Result queryPayTypeByWayCode(String wayCode){
        if (StrUtil.isNotBlank(wayCode) && wayCode.trim().length() >5){
           List<DicItemVo> dicItemVoList =  payWayService.queryPayTypeDic(wayCode.toUpperCase());
           return Result.ok(dicItemVoList);
        }
        return Result.fail(SysCode.REQUEST_PARAM_ERROR);
    }
    /**
     * 新增支付方式配置
     */
    @PostMapping("/payWay/create")
    public Result createPayWay(@RequestBody @Validated PayWayConfigRequest payWayConfigRequest){
        payWayService.createPayWayConfig(payWayConfigRequest);
        return Result.ok();
    }
}
